<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.Connection"%>
<%@page import="jobportal.Dbconn"%>
<%@ page import="java.io.*" %>

<%@ page import="java.util.zip.*"%>
<%
            String pic = null;
            String dest = "../student/wordresume.jsp";
            //RequestDispatcher rd=request.getRequestDispatcher("../student/wordresume.jsp");
            String username = (String) session.getAttribute("username");
            String table = "", col = "", pk_col = "", uname = "";
            if (session.getAttribute("role").equals("student")) {
                table = "resume";
                pk_col = "username";
                col = "file";
            } else if (session.getAttribute("role").equals("pto")) {
                table = "college_details";
                pk_col = "pto_email";
                col = "photo";
            } else if (session.getAttribute("role").equals("company")) {
                table = "company_details";
                col = "logo";
                pk_col = "comp_email";
            } else if (session.getAttribute("role").equals("admin")) {
                table = "admin";
                col = "photo";
                pk_col = "name";
            } else {
                session.invalidate();
                response.sendRedirect("../index.jsp");
            }


            try {
                String saveFile = "";
                String contentType = request.getContentType();
                if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0)) {
                    DataInputStream in = new DataInputStream(request.getInputStream());
                    int formDataLength = request.getContentLength();
                    byte dataBytes[] = new byte[formDataLength];
                    int byteRead = 0;
                    int totalBytesRead = 0;
                    while (totalBytesRead < formDataLength) {
                        byteRead = in.read(dataBytes, totalBytesRead, formDataLength);
                        totalBytesRead += byteRead;
                    }
                    String file = new String(dataBytes);
                    saveFile = file.substring(file.indexOf("filename=\"") + 10);
                    saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
                    saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1, saveFile.indexOf("\""));
                    int lastIndex = contentType.lastIndexOf("=");
                    String boundary = contentType.substring(lastIndex + 1, contentType.length());
                    int pos;
                    pos = file.indexOf("filename=\"");
                    pos = file.indexOf("\n", pos) + 1;
                    pos = file.indexOf("\n", pos) + 1;
                    pos = file.indexOf("\n", pos) + 1;
                    int boundaryLocation = file.indexOf(boundary, pos) - 4;
                    int startPos = ((file.substring(0, pos)).getBytes()).length;
                    int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;
                    File ff = new File(saveFile);
                    FileOutputStream fileOut = new FileOutputStream(ff);
                    fileOut.write(dataBytes, startPos, (endPos - startPos));
                    fileOut.flush();
                    fileOut.close();
                    //Successfully uploaded file to object

                    out.println(saveFile);
                    FileInputStream fis;
                    File f = new File(saveFile);
                    Connection con = Dbconn.getConn();
                    String sql = "update " + table + " set " + col + "=? where " + pk_col + "=?";
                    PreparedStatement psmnt = con.prepareStatement(sql);
                    fis = new FileInputStream(f);
                    psmnt.setBinaryStream(1, (InputStream) fis, (int) (f.length()));
                    psmnt.setString(2, username);
                    int s = psmnt.executeUpdate();
                    if (s > 0) {
                        request.setAttribute("status", "Resume uploaded sucessfully");
                        response.sendRedirect(dest + "?status=Resume uploaded sucessfully");
                        //rd.forward(request, response);
                    } else {
                        request.setAttribute("status", "resume upload failed");
                        response.sendRedirect(dest + "?status=resume upload failed");
                    }

                    //rd.forward(request, response);
                }
            } catch (FileNotFoundException fe) {
                request.setAttribute("status", "File Not Found");
                response.sendRedirect(dest + "?status=File Not Found");
                //rd.forward(request, response);
            } catch (Exception e) {
               if (e.toString().contains("com.mysql.jdbc.PacketTooBigException")) {
                    dest = dest + "?status=File is too large! Only 1Mb is allowed";
                    response.sendRedirect(dest);
                } else {
                    // request.setAttribute("status", "file updation failed::" + e);
                    //dest=dest+"?status=file updation failed::" + e.toString();
                    
                    response.sendRedirect(dest + "?status=" + e);
                }
            }

%>
